package com.example.comzg24_2.login

import androidx.lifecycle.viewModelScope
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.base.url.entity.CodeEntity
import com.example.base.url.entity.LoginEntity
import com.example.base.url.entity.Res
import com.example.base.url.mvvm.BaseViewModel
import com.example.base.url.mvvm.SingLiveData
import kotlinx.coroutines.launch

class LoginViewModel :BaseViewModel() {
    val loginSuccess = SingLiveData<Res<LoginEntity>>()
    val loginFail = SingLiveData<String>()
    fun Login(account:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"]=account
                map["password"]=password
                loginSuccess.postValue(apiService.Login(mapbody(map)))
            }catch (e:Exception){
                loginFail.postValue(e.message.toString())
            }
        }
    }


    val codeSuccess=SingLiveData<Res<CodeEntity>>()
    val codeFail = SingLiveData<String>()
    fun CodeLogin(phone:String){
        viewModelScope.launch {
            try {
                codeSuccess.postValue(apiService.codeLogin(phone))
            }catch (e:Exception){
                codeFail.postValue(e.message.toString())
            }
        }
    }

    val upDataSuccess=SingLiveData<Res<Boolean>>()
    val upDataFail = SingLiveData<String>()

    fun UpDataLogin(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"]=account
                map["captcha"]=captcha
                map["password"]=password
                upDataSuccess.postValue(apiService.upDataLogin(mapbody(map)))
            }catch (e:Exception){
                upDataFail.postValue(e.message.toString())
            }
        }
    }

}